//
//  VisitorView.swift
//  mySwift
//
//  Created by 高赛 on 16/9/14.
//  Copyright © 2016年 高赛. All rights reserved.
//

import UIKit

// 定义代理
protocol VisitorViewDelegate: NSObjectProtocol{
    // 登录方法回调
    func loginBtnWillClick()
    // 注册方法回调
    func registerBtnWillClick()
}


class VisitorView: UIView {
    
    // 定义一个代理
    weak var delegate: VisitorViewDelegate?
    
    /*
     设置未登录界面
    */
    func setupVisitorInfo(isHome:Bool, imageName: String, message: String){
        iconView.isHidden = !isHome
        homeIcon.image = UIImage(named: imageName)
        messageLabel.text = message
        // 判断是否需要执行动画
        if isHome {
            startAnimation()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.white
        
        // 1.添加子控件
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        // 2 布局子控件
        // 2.1 设置背景
        let _ = iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        // 2.1.1 设置蒙版 填充
        let _ = maskBGView.xmg_Fill(referView: self)
        // 2.2 设置小房子
        let _ = homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        // 2.3 设置文本
        let _ = messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        
        /*
         item:A控件
         attribute:a属性
         toItem:B控件
         attribute:b属性
         relatedBy:关系
         mulitplier:C倍数
         constant:D偏移
         A控件的a属性等于(大于/小于)B控件的b属性乘C加D
        */
        let widthCons = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(widthCons)
        
        //2.4 设置按钮
        let _ = registerBtn.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100,height:30), offset: CGPoint(x:0 ,y:20))
        let _ = loginBtn.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width:100 ,height: 30), offset: CGPoint(x:0 ,y : 20))
    }
    
    // MARK - 内部控制方法
    private func startAnimation(){
        // 1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        // 该属性默认为yes,代表动画只要完毕就移除
        anim.isRemovedOnCompletion = false
        // 3.将动画添加到控件上
        iconView.layer.add(anim, forKey: nil)
    }
     @objc private func loginBtnClick(){
        delegate?.loginBtnWillClick()
    }
    @objc private func registerBtnClick(){
        delegate?.registerBtnWillClick()
    }
    
    // MARK 懒加载
    // 转盘
    private lazy var iconView : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    // 图标
    private lazy var homeIcon : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    // 信息
    private lazy var messageLabel : UILabel = {
        let label = UILabel()
        label.text = "信息label"
        label.textAlignment = NSTextAlignment.center
        label.numberOfLines = 0
        label.textColor = UIColor.lightGray
        return label
    }()
    // 登陆按钮
    private lazy var loginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.lightGray, for: [])
        btn.setTitle("登陆", for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
        btn.addTarget(self, action: #selector(loginBtnClick), for: UIControlEvents.touchUpInside)
        return btn
    }()
    // 注册按钮
    private lazy var registerBtn : UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.orange, for: UIControlState.normal)
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
        btn.addTarget(self, action: #selector(registerBtnClick), for: UIControlEvents.touchUpInside)
        return btn
    }()
    // 背景图
    private lazy var maskBGView : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()
    
    // Swift 推荐我们自定义一个控件,要么用纯代码,要么用xib/stroyboard
    required init?(coder aDecoder: NSCoder) {
        // 如果通过xib来创建该类就会崩溃
        fatalError("init(coder:) has not been implemented")
    }
}
